//
//  ActionSheetConfig.swift
//  CWActionSheet
//
//  Created by chenwei on 2017/8/31.
//  Copyright © 2017年 cwwise. All rights reserved.
//

import UIKit

private extension UIColor {
    convenience init(vhl_hex6: UInt32, alpha: Float = 1) {
        let divisor = CGFloat(255)
        let red     = CGFloat((vhl_hex6 & 0xFF0000) >> 16) / divisor
        let green   = CGFloat((vhl_hex6 & 0x00FF00) >> 8) / divisor
        let blue    = CGFloat((vhl_hex6 & 0x0000FF) >> 0) / divisor
        self.init(red: red, green: green, blue: blue, alpha: CGFloat(alpha))
    }
}

extension UIImage {
    convenience init?(vhl_color: UIColor?, size: CGSize = CGSize(width: 10, height: 10)) {
        guard let nColor = vhl_color else {
            return nil
        }
        
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.main.scale)
        
        let context = UIGraphicsGetCurrentContext()
        context?.setFillColor(nColor.cgColor)
        context?.fill(rect)
        
        self.init(cgImage:(UIGraphicsGetImageFromCurrentImageContext()?.cgImage!)!)
        UIGraphicsEndImageContext()
    }
}

/** actionSheet 底部固定按钮 排列方式 */
public enum VHLActionSheetBottomButtonArrangement {
    case vertical
    case horizontal
}
/** actionSheet button Index 对应的 button 类型*/
public enum VHLActionSheetButtonIndexType: Int {
    case ok     = 999
    case cancel = 0
    case nomal  = -1
    // other
}

/// ActionSheetView配置
public class VHLActionSheetConfig {
    // MARK: 属性
    
    /// 阴影背景视图
    /// 模糊背景模糊范围. 范围越大越模糊。
    public var bgCoverRadius: CGFloat = 8.0
    /// 阴影背景颜色
    public var bgCoverColor: UIColor?
    /// 阴影透明度
    public var bgCoverAlpha: CGFloat = 0.30
    
    /// actionSheet 背景颜色
    public var actionSheetBackgroundColor: UIColor = UIColor.white
    /// actionSheet 背景圆角
    public var actionSheetCornerRadius: CGFloat = 0
    /// actionSheet 圆角范围
    public var actionSheetCorners: UIRectCorner = [.allCorners]

    /// action 视图外边缘边距
    public var outerEdgeInsets: UIEdgeInsets = UIEdgeInsets.zero
    /// action 视图内边缘边距
    public var innerEdgeInsets: UIEdgeInsets = UIEdgeInsets.zero
    
    /// 标题颜色 默认0x888888
    public var titleColor: UIColor = UIColor(vhl_hex6: 0x888888)
    /// 标题字体 默认 `[UIFont systemFontOfSize:14.0f]`
    public var titleFont: UIFont = UIFont.systemFont(ofSize: 17)
    /// 标题行数 默认为0，即不限制
    public var titleLinesNumber: Int = 0
    
    /// 副标题颜色
    public var subTitleColor: UIColor = UIColor(vhl_hex6: 0x888888)
    /// 副标题字体
    public var subTitleFont: UIFont = UIFont.systemFont(ofSize: 14)
    /// 副标题行数 默认为0，即不限制
    public var subTitleLinesNumber: Int = 0
    
    /// 标题 Insets
    public var titleEdgeInsets: UIEdgeInsets = UIEdgeInsets(top: 0, left: 4, bottom: 8, right: 4)
    // 副标题 Insert
    public var subtitleEdgeInsets: UIEdgeInsets = UIEdgeInsets(top: 0, left: 4, bottom: 8, right: 4)
    
    // 按钮：3种按钮，取消按钮，普通按钮，警告提示按钮
    
    /// 按钮文字颜色
    public var buttonTitleColor: UIColor = UIColor.black
    /// 按钮字体 默认 `[UIFont systemFontOfSize:17.0f]`
    public var buttonFont: UIFont = UIFont.systemFont(ofSize: 17)
    /// 按钮圆角
    public var buttonCornerRadius: CGFloat = 0.0
    /// 按钮高度
    public var buttonHeight: CGFloat = 50.0
    /// 按钮普通背景颜色
    public var buttonBGColor: UIColor = UIColor.white
    /// 按钮高亮背景颜色
    public var buttonHighlightdBGColor: UIColor =  UIColor(red:0.97, green:0.97, blue:0.97, alpha:1.00)
    
    // MARK: 固定按钮
    // 底部固定按钮高度
    public var fixedButtonHeight: CGFloat = 50.0
    // 底部固定按钮圆角
    public var fixedButtonCornerRadius: CGFloat = 0.0
    // 底部固定按钮排列方式。并排和竖排
    public var bottomButtonArrangement: VHLActionSheetBottomButtonArrangement = .vertical
    /// 取消按钮顶部分隔线高度
    public var cancelButtonTopLineHeight: CGFloat = 10
    /// 取消按钮顶部分隔线背景颜色
    public var cancelButtonTopLineColor: UIColor = UIColor(red:0.93, green:0.93, blue:0.93, alpha:1.00)
    
    /// cancel button title color
    public var okButtonTitleColor: UIColor = UIColor.black
    /// cancel button 普通背景颜色
    public var okButtonBGColor: UIColor = UIColor(red:0.97, green:0.97, blue:0.97, alpha:1.00)
    /// cancel button 高亮背景颜色
    public var okButtonHighlightdBGColor: UIColor = UIColor(red:0.97, green:0.97, blue:0.97, alpha:0.7)
    
    /// cancel button title color
    public var cancelButtonTitleColor: UIColor = UIColor.black
    /// cancel button 普通背景颜色
    public var cancelButtonBGColor: UIColor = UIColor(red:0.97, green:0.97, blue:0.97, alpha:1.00)
    /// cancel button 高亮背景颜色
    public var cancelButtonHighlightdBGColor: UIColor = UIColor(red:0.97, green:0.97, blue:0.97, alpha:0.7)
    
    // MARK: 其他按钮
    /// destructive 破坏性提示按钮标题颜色
    public var destructiveButtonTitleColor: UIColor = UIColor.red
    /// destructive 破坏性提示按钮背景颜色
    public var destructiveButtonColor: UIColor = UIColor.white
    /// destructive 破坏性提示按钮高亮背景颜色
    public var destructiveButtonHighlightdColor: UIColor = UIColor(red:0.97, green:0.97, blue:0.97, alpha:1.00)
    
    /// 分割线颜色
    public var separatorColor: UIColor = UIColor.clear //UIColor(red:0.97, green:0.97, blue:0.97, alpha:1.00)
    
    /// 动画时间
    public var animationDuration: TimeInterval = 0.32
    /// 是否可以点击其他区域
    public var canTouchToDismiss: Bool = true
    
    public weak var actionSheetView: VHLActionSheetView?        // 保留上级 actionSheetView，用于刷新
    
    public enum VHLActionCellStyle: Int {
        case light = 1
        case dark = 2
        case system = 3
    }
    public var cellStyle: VHLActionCellStyle = .system
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    // MARK: 初始化
    public init() {
        bgCoverRadius = 0.0
        bgCoverColor = .black
        bgCoverAlpha = 0.30
        
        actionSheetCornerRadius = 10         // 圆角
        
        // 内边距
        titleEdgeInsets = UIEdgeInsets(top: 10, left: 4, bottom: 5, right: 4)
        subtitleEdgeInsets = UIEdgeInsets(top: 0, left: 4, bottom: 5, right: 4)
        
        buttonCornerRadius = 5
        fixedButtonCornerRadius = 20
        cancelButtonTopLineColor = UIColor.clear
        
        buttonHeight = 60
        fixedButtonHeight = 44.0
        bottomButtonArrangement = .horizontal
        
        cancelButtonTopLineHeight = 16
        
        // 外边距 底部 15 ，异形屏 34
        if #available(iOS 11.0, *) {
            let bottom = max(UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 16, 16)
            if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad) {      // iPad 左右
                innerEdgeInsets = UIEdgeInsets(top: 16, left: 16, bottom: bottom, right: 16)
                outerEdgeInsets = UIEdgeInsets(top: max(UIApplication.shared.keyWindow?.safeAreaInsets.top ?? 15, 15), left: 60, bottom: max(UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 15, 15), right: 60)
                actionSheetCorners = [.allCorners]
            } else {
                innerEdgeInsets = UIEdgeInsets(top: 16, left: 16, bottom: bottom, right: 16)
                outerEdgeInsets = .zero
                actionSheetCorners = [.topLeft, .topRight]
            }
        } else {
            innerEdgeInsets = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
            outerEdgeInsets = .zero
            actionSheetCorners = [.topLeft, .topRight]
        }
        
    }
}
